{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e98c4698",
   "metadata": {},
   "source": [
    "### 列表的查询操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d42ecd7e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.正向索引从0到N-1,举例lst[0]\n",
    "# 2.逆向索引从-N到-1,举例lst[-N]\n",
    "# 指定索引不存,抛出IndexError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "55d02f0e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "98\n"
     ]
    }
   ],
   "source": [
    "lst = ['hello','world',98,'hello','world',234]\n",
    "# 获取索引为2的元素\n",
    "print(lst[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8bfffaa5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n"
     ]
    }
   ],
   "source": [
    "# 获取索引为 -3 的元素\n",
    "print(lst[-3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6eebc2cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取索引为10的元素\n",
    "\n",
    "# print(lst[10])\n",
    "# 会报错，因为不会在范围内，"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6e62371",
   "metadata": {},
   "source": [
    "### 获取列表中的多个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "cb1d7c39",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 切片操作\n",
    "# 列表名[start : stop : step]\n",
    "# 范围: [start,stop]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8ee98485",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[20, 30, 40, 50, 60]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst = [10,20,30,40,50,60,70,80]\n",
    "lst[1:6:1]\n",
    "# step = 步伐"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7d6aeb1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "140327726689152"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "97043abb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "140327992500032"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst2 = lst[1:6:1]\n",
    "id(lst2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d92f1591",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[20, 30, 40, 50, 60]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst[1:6]\n",
    "# 不写步长的话，默认步长为1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "50e77cae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[20, 30, 40, 50, 60]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst[1:6:]\n",
    "# 不写的话，默认也是1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "092eaf01",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[20, 40, 60]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst[1:6:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b6fcb0fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 30, 50]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst[:6:2]\n",
    "# 默认从0开始"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "1bafd621",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[20, 40, 60, 80]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst[1::2]\n",
    "# 如果不写stop的话，默认会切到最后一个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "94e784ff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10, 20, 30, 40, 50, 60, 70, 80]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[80, 70, 60, 50, 40, 30, 20, 10]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# step步长为负数的情况\n",
    "print(lst)\n",
    "lst[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "606f493f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[80, 70, 60, 50, 40, 30, 20, 10]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst[7::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "52e21fe3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[70, 50, 30]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst[6:0:-2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9287e794",
   "metadata": {},
   "source": [
    "### 判断指定元素在列表中是否存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85e5e970",
   "metadata": {},
   "outputs": [],
   "source": [
    "# in or not in "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "bebcfa54",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'p' in 'python'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "ed2d5e9d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'k' not in 'python'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "881304f0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst = [10,20,'python','hello']\n",
    "10 in lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "f71a4bae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "100 in lst "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "42755366",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10 not in lst "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "2b2e1219",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "20\n",
      "python\n",
      "hello\n"
     ]
    }
   ],
   "source": [
    "for item in lst:\n",
    "    print(item)\n",
    "    # item 可以将列表当中的元素依次进行输出\n",
    "    # 列表元素的遍历"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f7e1ab0",
   "metadata": {},
   "source": [
    "### 列表元素的增加操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "126b6e7e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10, 20, 30] 140328566710784\n",
      "[10, 20, 30, 100] 140328566710784\n"
     ]
    }
   ],
   "source": [
    " # append，向列表末尾添加一个元素\n",
    "lst = [10,20,30]\n",
    "print(lst,id(lst))\n",
    " # append 添加后\n",
    "lst.append(100)\n",
    "print(lst,id(lst))\n",
    "\n",
    "# id是一致的。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "cf6a3c7f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10,\n",
       " 20,\n",
       " 30,\n",
       " 100,\n",
       " ['hello', 'world'],\n",
       " 'hello',\n",
       " 'world',\n",
       " ['hello', 'world'],\n",
       " ['hello', 'world']]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# extend()\n",
    "# 在列表中至少添加一个元素\n",
    "lst2 = [\"hello\",\"world\"]\n",
    "lst.append(lst2)\n",
    "lst\n",
    "# 将lst作为一个元素添加到列表的末尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "4ca8b6af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10,\n",
       " 20,\n",
       " 30,\n",
       " 100,\n",
       " ['hello', 'world'],\n",
       " 'hello',\n",
       " 'world',\n",
       " ['hello', 'world'],\n",
       " ['hello', 'world'],\n",
       " ['hello', 'world'],\n",
       " 'hello',\n",
       " 'world']"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# extend() 一次性添加多个元素\n",
    "lst2 = [\"hello\",\"world\"]\n",
    "lst.extend(lst2)\n",
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "e6f478fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10,\n",
       " 20,\n",
       " 30,\n",
       " 100,\n",
       " ['hello', 'world'],\n",
       " 'hello',\n",
       " 'world',\n",
       " ['hello', 'world'],\n",
       " ['hello', 'world'],\n",
       " ['hello', 'world'],\n",
       " 'hello',\n",
       " 'world',\n",
       " 10,\n",
       " 20,\n",
       " 30,\n",
       " 100,\n",
       " ['hello', 'world'],\n",
       " 'hello',\n",
       " 'world',\n",
       " ['hello', 'world'],\n",
       " ['hello', 'world'],\n",
       " ['hello', 'world'],\n",
       " 'hello',\n",
       " 'world']"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# insert()，在列表中任意位置添加一个元素\n",
    "lst.extend(lst)\n",
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "230cea81",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10,\n",
       " 20,\n",
       " 88,\n",
       " 30,\n",
       " 100,\n",
       " ['hello', 'world'],\n",
       " 'hello',\n",
       " 'world',\n",
       " ['hello', 'world'],\n",
       " ['hello', 'world'],\n",
       " ['hello', 'world'],\n",
       " 'hello',\n",
       " 'world',\n",
       " 10,\n",
       " 20,\n",
       " 30,\n",
       " 100,\n",
       " ['hello', 'world'],\n",
       " 'hello',\n",
       " 'world',\n",
       " ['hello', 'world'],\n",
       " ['hello', 'world'],\n",
       " ['hello', 'world'],\n",
       " 'hello',\n",
       " 'world']"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst.insert(2,88)\n",
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "c7f02a34",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[True, True, False, 'hello']"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片，在列表任意位置上至少添加任何一个元素\n",
    "lst3 = [True,False,'hello']\n",
    "# 在任意位置上添加N多个元素\n",
    "lst[1:] = lst3\n",
    "lst"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9eb7af79",
   "metadata": {},
   "source": [
    "### 列表元素的删除操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "9c860872",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 20, 40, 50, 60, 30]"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst = [10,20,30,40,50,60,30]\n",
    "lst.remove(30)\n",
    "lst\n",
    "# 从列表中移除一个元素，如果有重复元素只会移除第一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "938f2e64",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 比如移除元素不存在\n",
    "# lst.remove(100)\n",
    "# 那么就会报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "ecb91199",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 40, 50, 60, 30]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pop()根据索引移除元素\n",
    "lst.pop(1)\n",
    "lst\n",
    "# 如果指定索引不存在的话就无法移除会报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "e7adca3c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 20, 40, 50, 60]"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst.pop()\n",
    "lst\n",
    "# 如果不指定会默认将最后一个元素删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "3e804a2b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10, 20, 40, 50, 60]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[20, 40]"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片操作，删除至少一个元素，将产生一个新的列表对象\n",
    "new_lst = lst[1:3]\n",
    "print(lst)\n",
    "new_lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "ea3a6fcb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 50, 60]"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 不产生新的列表对象，而是删除原列表中的内容\n",
    "lst[1:3] = []\n",
    "lst\n",
    "# 其实并不是真正意义的删除，而是用空列表去替代"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "a0091ee9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    }
   ],
   "source": [
    "# clear(),清除列表中的所有元素\n",
    "lst.clear()\n",
    "print(lst)\n",
    "# 会发现没有元素了，都清空了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "8f5571b3",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'lst' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m/var/folders/0y/9ngk9zq509d_968nb4blyvrm0000gn/T/ipykernel_997/2824930161.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# del语句将列表直接删除\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mdel\u001b[0m \u001b[0mlst\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      3\u001b[0m \u001b[0;31m#lst\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'lst' is not defined"
     ]
    }
   ],
   "source": [
    "# del语句将列表直接删除\n",
    "del lst\n",
    "#lst\n",
    "# 因为已经没有元素了。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2032d4d",
   "metadata": {},
   "source": [
    "### 列表元素的修改操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "6567ba5d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 20, 100, 40]"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst = [10,20,30,40]\n",
    "# 一次修改一个值\n",
    "lst[2] = 100\n",
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "adc35412",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 200, 400, 500, 600, 40]"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst[1:3] = [200,400,500,600]\n",
    "lst\n",
    "# 修改列表中多个值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e33336c2",
   "metadata": {},
   "source": [
    "### 列表元素的排序操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "9e3d0e82",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([20, 40, 10, 98, 54], 140328276187840)"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst = [20,40,10,98,54]\n",
    "lst,id(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "5fe46290",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([10, 20, 40, 54, 98], 140328276187840)"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 开始排序\n",
    "# 调用sort方法，默认是升序\n",
    "lst.sort()\n",
    "lst,id(lst)\n",
    "# 下面是排序之后的。\n",
    "# 会发现id都是一样的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "db1fc6e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[98, 54, 40, 20, 10]"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 关键字参数，将列表中的元素进行降序操作\n",
    "lst.sort(reverse = True)\n",
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "006aaea7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 20, 40, 54, 98]"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst.sort(reverse = False)\n",
    "lst\n",
    "# 如果是False，就会执行升序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "b9c80bbf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[20, 40, 10, 98, 54]"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用内置函数 sortde 对列表进行排序，将产生一个新的列表对象\n",
    "lst = [20,40,10,98,54]\n",
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "3c8b3ac5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[20, 40, 10, 98, 54]\n",
      "[10, 20, 40, 54, 98]\n"
     ]
    }
   ],
   "source": [
    "new_lst = sorted(lst)\n",
    "print(lst)\n",
    "print(new_lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "8eb6af27",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[98, 54, 40, 20, 10]"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 指定关键字参数，实现列表元素的降序排序\n",
    "desc_list = sorted(lst,reverse = True)\n",
    "desc_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "d223e0cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 区别？\n",
    "# sort方法是对原列表进行一个排序操作\n",
    "# 内置函数sorted()是产生一个新的列表对象，原列表不发生改变"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1c6af8c",
   "metadata": {},
   "source": [
    "### 列表生成式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "e38b99b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst = [i for i in range(1,10)]\n",
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "33014167",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25, 36, 49, 64, 81]"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst = [i*i for i in range(1,10)]\n",
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "f687ca47",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 4, 6, 8, 10]"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst2 = [i*2 for i in range(1,6)]\n",
    "lst2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52c66e97",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
